BlankClassSoDocsWillBeGenerated

version(D_Ddoc)
class BlankClassSoDocsWillBeGenerated

Examples

This example is for the DEFAULT constraint. The table in SQL can be created by $(D $(D sql CREATE TABLE Hotels ( Id INTEGER NOT NULL PRIMARY KEY, Name TEXT, City TEXT DEFAULT ot available); ))

Default in this package was made for foreign key interactions. With that in mind I know in this example it seems like I should just set the default value in the setter but then my package would not know what default you want for a foreign key update rule or delete rule.

1 import db_constraints;
2 
3 class Hotel
4 {
5     private int _Id;
6     // marking Id with not null and primary key
7     @NotNull @PrimaryKeyColumn
8     @property int Id()
9     {
10         return _Id;
11     }
12     @property void Id(int value)
13     {
14         setter(_Id, value);
15     }
16 
17 
18     private string _Name;
19     @property string Name()
20     {
21         return _Name;
22     }
23     @property void Name(string value)
24     {
25         setter(_Name, value);
26     }
27 
28     private string _City;
29     // marking city with its default
30     @Default!("not available")
31     @property string City()
32     {
33         return _City;
34     }
35     @property void City(string value)
36     {
37         setter(_City, value);
38     }
39 
40     this(int Id_, string Name_, string City_)
41     {
42         this._Id = Id_;
43         this._Name = Name_;
44         this._City = City_;
45         initializeKeyedItem();
46     }
47     this(int Id_, string Name_)
48     {
49         this._Id = Id_;
50         this._Name = Name_;
51         // using GetDefault will get the value you
52         // placed in the Default attribute
53         this._City = GetDefault!(Hotel, "City");
54         initializeKeyedItem();
55     }
56 
57     mixin KeyedItem!();
58 }
59 
60 // make sure Hotel.City has a default attribute
61 assert(hasDefault!(Hotel, "City"));
62 // the default attribute has value not available
63 assert(GetDefault!(Hotel, "City") == "not available");
64 
65 auto i = new Hotel(1, "Kyjev", "Bratislava");
66 assert(i.City == "Bratislava");
67 // since City is not included in this constructor we use the default
68 auto j = new Hotel(2, "Slovan");
69 assert(j.City == "not available");

Meta